package net.shao.activity;

import android.location.Geocoder;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.TextView;

import com.baidu.location.Address;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.model.inner.GeoPoint;
import com.socks.library.KLog;

import net.shao.R;
import net.shao.base.BaseActivity;

import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.io.IOException;
import java.util.List;


public class BaiduMapActivity extends BaseActivity {

//    @ViewInject(R.id.mapView)
//    private MapView mapView;

    @ViewInject(R.id.tv_baiduLat)
    private TextView tv_baiduLat;

    private BaiduMap baiduMap;
    // 定位相关
    private LocationClient locationClient;
    private boolean isFirstLoc = true; // 是否首次定位
    private MyLocationListenner myLocationListenner = new MyLocationListenner();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        x.view().inject(this);


//        //地图初始化
//        baiduMap = mapView.getMap();
//        //开启定位图层
//        baiduMap.setMyLocationEnabled(true);
//        // 定位初始化
        locationClient = new LocationClient(this);
        locationClient.registerLocationListener(myLocationListenner);
        LocationClientOption locationClientOption = new LocationClientOption();
        locationClientOption.setOpenGps(true);//打开GPS
        locationClientOption.setCoorType("bd0911");//设置坐标类型
        locationClientOption.setScanSpan(1000);
        locationClient.setLocOption(locationClientOption);
        locationClient.start();

    }


    /**
     * 定位SDK监听函数
     */
    public class MyLocationListenner implements BDLocationListener {

        @Override
        public void onReceiveLocation(BDLocation location) {
            // map view 销毁后不在处理新接收的位置
            if (location == null) {
                return;
            }
            KLog.i("location>>>" + location.getLatitude());
            KLog.i("location>>>" + location.getLongitude());
            double latitude = location.getLatitude();
            double longitude = location.getLongitude();
            Geocoder mGeocoder = new Geocoder(BaiduMapActivity.this);
            GeoPoint geo = new GeoPoint((int) (latitude * 1E6), (int) (longitude * 1E6));
            int x = (int) geo.getLatitudeE6();//得到geo 纬度，单位微度(度* 1E6)
            double x1 = ((double)x)/1000000;
            int y = (int) geo.getLongitudeE6();//得到geo 经度，单位微度(度* 1E6)
            double y1 = ((double) y) / 1000000;

            try {
                List<android.location.Address> addressesList = mGeocoder.getFromLocation(latitude,longitude,0);

                for (int i =0;i<addressesList.size();i++){
                    KLog.i("addressesList>>>"+addressesList.get(i));
                }

                android.location.Address address = null;

                if(!addressesList.isEmpty()){
                    address = addressesList.get(0);
                }

                if ( address!=null && !TextUtils.isEmpty(address.getLocality())){
                    tv_baiduLat.setText(address.getLocality());

                }


            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }


//
//    @Override
//    protected void onDestroy() {
//        super.onDestroy();
//        //在activity执行onDestroy时执行mMapView.onDestroy()，实现地图生命周期管理
//        // 退出时销毁定位
//        locationClient.stop();
//        // 关闭定位图层
//        baiduMap.setMyLocationEnabled(false);
//        mapView.onDestroy();
//        mapView = null;
//    }
//
//    @Override
//    protected void onResume() {
//        super.onResume();
//        //在activity执行onResume时执行mMapView. onResume ()，实现地图生命周期管理
//        mapView.onResume();
//    }
//
//    @Override
//    protected void onPause() {
//        super.onPause();
//        //在activity执行onPause时执行mMapView. onPause ()，实现地图生命周期管理
//        mapView.onPause();
//    }


}
